x86/fpu: create lazy and non-lazy FPU restore functions
authorWei Huang <wei.huang2@amd.com>
Mon, 9 May 2011 10:39:23 +0000 (11:39 +0100)
committerWei Huang <wei.huang2@amd.com>
Mon, 9 May 2011 10:39:23 +0000 (11:39 +0100)
Currently Xen relies on #NM (via CR0.TS) to trigger FPU context
restore. But not all FPU state is tracked by TS bit. This function
creates two FPU restore functions: vcpu_restore_fpu_lazy() and
vcpu_restore_fpu_eager(). vcpu_restore_fpu_lazy() is still used when
#NM is triggered. vcpu_restore_fpu_eager(), as a comparision, is
called for vcpu which is being scheduled in on every context
switch. To minimize restore overhead, it creates a flag,
nonlazy_xstate_used, to control non-lazy restore.

Signed-off-by: Wei Huang <wei.huang2@amd.com>
xen/arch/x86/domain.c
xen/arch/x86/hvm/svm/svm.c
xen/arch/x86/hvm/vmx/vmx.c
xen/arch/x86/i387.c
xen/arch/x86/traps.c
xen/include/asm-x86/domain.h
xen/include/asm-x86/i387.h

index 8c19b9e7d3af08a1f0c4a20497677c0d5bcff59b..8bb0f5dd81cca4e22cd8ab7adb12437dc5c873f1 100644 (file)
@@ -1566,6 +1566,7 @@ static void __context_switch(void)
         memcpy(stack_regs, &n->arch.user_regs, CTXT_SWITCH_STACK_BYTES);
         if ( xsave_enabled(n) && n->arch.xcr0 != get_xcr0() )
             set_xcr0(n->arch.xcr0);
+        vcpu_restore_fpu_eager(n);
         n->arch.ctxt_switch_to(n);
     }
 
index 0ed05cbcc105b8c835269203db569ec4c4584d35..197fa2c3a819a216b0455f81d70f46967b9df4b8 100644 (file)
@@ -348,7 +348,7 @@ static void svm_fpu_enter(struct vcpu *v)
 {
     struct vmcb_struct *vmcb = v->arch.hvm_svm.vmcb;
 
-    vcpu_restore_fpu(v);
+    vcpu_restore_fpu_lazy(v);
     vmcb_set_exception_intercepts(
         vmcb, vmcb_get_exception_intercepts(vmcb) & ~(1U << TRAP_no_device));
 }
index b9aa49b59986e7157bba3df07121f928e5bf08b4..996bbfe9a8bb124179608dc4e8bc41bae06ecda7 100644 (file)
@@ -612,7 +612,7 @@ static int vmx_load_vmcs_ctxt(struct vcpu *v, struct hvm_hw_cpu *ctxt)
 
 static void vmx_fpu_enter(struct vcpu *v)
 {
-    vcpu_restore_fpu(v);
+    vcpu_restore_fpu_lazy(v);
     v->arch.hvm_vmx.exception_bitmap &= ~(1u << TRAP_no_device);
     vmx_update_exception_bitmap(v);
     v->arch.hvm_vmx.host_cr0 &= ~X86_CR0_TS;
index 9350f734d519d37d3f485bbf26f75aa0abc419db..e5780ef81b9f27d2bbdff94eedbb290705e58a1d 100644 (file)
@@ -98,13 +98,13 @@ static inline void fpu_frstor(struct vcpu *v)
 /*      FPU Save Functions     */
 /*******************************/
 /* Save x87 extended state */
-static inline void fpu_xsave(struct vcpu *v, uint64_t mask)
+static inline void fpu_xsave(struct vcpu *v)
 {
     /* XCR0 normally represents what guest OS set. In case of Xen itself,
      * we set all accumulated feature mask before doing save/restore.
      */
     set_xcr0(v->arch.xcr0_accum);
-    xsave(v, mask);
+    xsave(v, v->arch.nonlazy_xstate_used ? XSTATE_ALL : XSTATE_LAZY);
     set_xcr0(v->arch.xcr0);    
 }
 
@@ -160,10 +160,25 @@ static inline void fpu_fsave(struct vcpu *v)
 /*******************************/
 /*       VCPU FPU Functions    */
 /*******************************/
+/* Restore FPU state whenever VCPU is schduled in. */
+void vcpu_restore_fpu_eager(struct vcpu *v)
+{
+    ASSERT(!is_idle_vcpu(v));
+    
+    /* save the nonlazy extended state which is not tracked by CR0.TS bit */
+    if ( v->arch.nonlazy_xstate_used )
+    {
+        /* Avoid recursion */
+        clts();        
+        fpu_xrstor(v, XSTATE_NONLAZY);
+        stts();
+    }
+}
+
 /* 
  * Restore FPU state when #NM is triggered.
  */
-void vcpu_restore_fpu(struct vcpu *v)
+void vcpu_restore_fpu_lazy(struct vcpu *v)
 {
     ASSERT(!is_idle_vcpu(v));
 
@@ -174,7 +189,7 @@ void vcpu_restore_fpu(struct vcpu *v)
         return;
 
     if ( xsave_enabled(v) )
-        fpu_xrstor(v, XSTATE_ALL);
+        fpu_xrstor(v, XSTATE_LAZY);
     else if ( v->fpu_initialised )
     {
         if ( cpu_has_fxsr )
@@ -204,7 +219,7 @@ void vcpu_save_fpu(struct vcpu *v)
     clts();
 
     if ( xsave_enabled(v) )
-        fpu_xsave(v, XSTATE_ALL);
+        fpu_xsave(v);
     else if ( cpu_has_fxsr )
         fpu_fxsave(v);
     else
index b57bbf56ca76b4b09911513fc01b2af56a771678..fc4ad842020dcfee3ef361c7ce1c55152cb000c9 100644 (file)
@@ -3198,7 +3198,7 @@ asmlinkage void do_device_not_available(struct cpu_user_regs *regs)
 
     BUG_ON(!guest_mode(regs));
 
-    vcpu_restore_fpu(curr);
+    vcpu_restore_fpu_lazy(curr);
 
     if ( curr->arch.pv_vcpu.ctrlreg[0] & X86_CR0_TS )
     {
index 095735ed8554f5cbd03a4be5ee79daddae2d8022..0488e655bb2d66f6ba347b31c5c03cc53036b7b5 100644 (file)
@@ -492,7 +492,10 @@ struct arch_vcpu
      * it explicitly enables it via xcr0.
      */
     uint64_t xcr0_accum;
-
+    /* This variable determines whether nonlazy extended state has been used,
+     * and thus should be saved/restored. */
+    bool_t nonlazy_xstate_used;
+    
     struct paging_vcpu paging;
 
 #ifdef CONFIG_X86_32
index f3a7df02292e4707a28cb9c1a34fdd5c70577878..27399c0bbfb484fe94b36838238c147dd55a5857 100644 (file)
@@ -14,7 +14,8 @@
 #include <xen/types.h>
 #include <xen/percpu.h>
 
-void vcpu_restore_fpu(struct vcpu *v);
+void vcpu_restore_fpu_eager(struct vcpu *v);
+void vcpu_restore_fpu_lazy(struct vcpu *v);
 void vcpu_save_fpu(struct vcpu *v);
 
 int vcpu_init_fpu(struct vcpu *v);